{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是一个画图的库，和numpy，pandas可以无缝衔接，anaconda已经集成。  \n",
    "jupyter对matplotlib的支持很好，可以直接在jupyter中显示画出来的图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在jupyter中需要加一行%matplotlib inline或%matplotlib notebook，这样图就能在jupyter中直接显示。在.py的python文件中就不要加了，会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2, 3, 4, 5, 6])\n",
    "b = np.array([1, 2, 3, 4, 5, 6])\n",
    "c = np.array([6, 5, 4, 3, 2, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (6, 6))\n",
    "plt.plot(a, b, 'o')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "plt.figure(figsize)中,figsize指定图的大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test1 尝试改变figsize\n",
    "改成(4, 4), (10, 6)试试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# YOUR CODE HERE\n",
    "plt.figure(figsize=(4,4))\n",
    "plt.plot(a, b, 'o')\n",
    "\n",
    "plt.figure(figsize=(10,6))\n",
    "plt.plot(a, b, 'o')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test2 尝试改变字符串\n",
    "尝试将'o'改成'o-', '.', '.-'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# o-\n",
    "# YOUR CODE HERE\n",
    "plt.plot(a, b, 'o-')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# .\n",
    "# YOUR CODE HERE\n",
    "plt.plot(a, b, '.')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# .-\n",
    "# YOUR CODE HERE\n",
    "plt.plot(a, b, '.-')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 改变线的颜色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test3\n",
    "尝试将color换成'blue', 'yellow', 'green'试试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# blue\n",
    "# YOUR CODE HERE\n",
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'blue')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# yellow\n",
    "# YOUR CODE HERE\n",
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'yellow')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# green\n",
    "# YOUR CODE HERE\n",
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'green')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## xlabel, ylabel, title"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用xlabel, ylabel, title增加x标签，y标签，标题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'red')\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.title(\"title\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对曲线增加label"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以对曲线增加label，然后显示图例，在plot函数中，加入label关键字，在最后加入plt.legend()即可显示出图例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'red', label = 'training')\n",
    "plt.plot(a, c, '-', color = 'blue', label = 'testing')\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.title(\"title\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "plt.legend()接受一个loc参数，值可以为  \n",
    "best  \n",
    "upper right  \n",
    "upper left  \n",
    "lower left  \n",
    "lower right  \n",
    "right  \n",
    "center left  \n",
    "center right  \n",
    "lower center  \n",
    "upper center  \n",
    "center  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (8, 4))\n",
    "plt.plot(a, b, '-', color = 'red', label = 'training')\n",
    "plt.plot(a, c, '-', color = 'blue', label = 'testing')\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.title(\"title\")\n",
    "plt.legend(loc = 'center')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 子图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "matplotlib可以在一张图中画多个子图，plt.subplot里面有三个数，第一个数表示这个大图里面画多少行，第二个数表示这个大图里面画多少列，第三个数表示，当前画的是第几个子图，比如下面我们要画两个子图，在同一行，所以就是1行，2列，subplot前两个数字就是12。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize = (10, 4))\n",
    "\n",
    "plt.subplot(121)\n",
    "plt.plot(a, b, '-', color = 'red', label = 'training')\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.legend(loc = 'right')\n",
    "\n",
    "plt.subplot(122)\n",
    "plt.plot(a, c, '-', color = 'blue', label = 'testing')\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.legend(loc = 'right')\n",
    "\n",
    "plt.suptitle(\"title\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# **这里介绍的只是Matplotlib强大功能的冰山一角，希望大家可以养成查阅官方文档的习惯**\n",
    "Matplotlib文档： https://matplotlib.org/3.1.1/index.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
